<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\BaseController;
use App\Service\Common\ShortUrlService;
use Illuminate\Http\Request;

class ShortUrlController extends BaseController
{
    //url转短链  WEB
    public function generateShortUrl(Request $request)
    {
        $code = $request->get('code', '');
        $url  = $request->get('url', '');
        if (!mb_strlen($url)) {
            return $this->fail(5, '参数不正确');
        }
        return $this->success(ShortUrlService::addUrl($url, $code));
    }

    public function getShortUrl($code)
    {
        //通过code获得URl   CONTROLLER
        $data = ShortUrlService::getShortUrl((string)$code);
        if (null === $data) {
            return response()->redirectTo('https://www.kuafood.com/');
        }
        return response()->redirectTo($data['url']);
    }

    public function index()
    {
        return view('common.short-url.index');
    }
}
